package stu.sup.service.impl;

import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import stu.sup.dto.BuycarDto;
import stu.sup.entity.*;
import stu.sup.mapper.BuycartMapper;
import stu.sup.mapper.ClassTableMapper;
import stu.sup.mapper.ProductMapper;
import stu.sup.mapper.UserLoginMapper;
import stu.sup.service.IBuycarService;
import stu.sup.service.IClassTableService;
import stu.sup.service.IPicInfoService;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;


/**
 * Created by HUAWEI on 2020/6/22.
 */
@Service
@Transactional
public class BuyCarServiceImpl implements IBuycarService {
    @Autowired
    private BuycartMapper buycartMapper;
    @Autowired
    private ProductMapper productMapper;
    @Autowired
    private UserLoginMapper userLoginMapper;
    @Autowired
    private IClassTableService classTableService;
    @Autowired
    private IPicInfoService picInfoService;
    @Override
    public Buycart updateBuycar(Product product,UserLogin userLogin) {
        BuycartExample example=new BuycartExample();
        BuycartExample.Criteria criteria=example.createCriteria();
        criteria.andProductIdEqualTo(product.getId());
        List<Buycart> list=buycartMapper.selectByExample(example);
        if (list==null||list.size()==0){
            Buycart buycart=new Buycart();
            buycart.setUserLoginId(userLogin.getId());
            buycart.setProductId(product.getId());
            buycart.setProductNum(1);
            buycart.setProductPrice(product.getProductPrice());
            buycart.setAddTime(new Date());
            buycartMapper.insert(buycart);
            return buycart;
        }
        else {
            for (Buycart b: list){
                if (b.getProductId()==product.getId()&&b.getUserLoginId()==userLogin.getId()){
                    b.setProductNum(b.getProductNum()+1);
                    b.setProductPrice(b.getProductPrice()*b.getProductNum());
                    buycartMapper.updateByPrimaryKey(b);
                    return b;
                }
            }
        }
        return null;
    }
    @Override
    public Buycart addProduct(Buycart buycart){
        buycartMapper.insert(buycart);
        return buycart;
    }

    @Override
    public boolean flagbuycar(Integer userId, Integer productId) {
        List<Buycart> list=buycartMapper.selectByUserId(userId);

        boolean flag=false;
        if(null==list || list.size()==0)
            flag = false;
        else{
            for (Buycart buycart : list) {
                if(buycart.getProductId().equals(productId)){
                    flag = true;
                    break;
                }
            }
        }
        return flag;
    }

    @Override
    public double Allprice(Integer UserId,List<Integer> buycarId) {
        List<Buycart> buycarts=buycartMapper.selectByUserId(UserId);
        double price=0;
        for (Buycart b:buycarts){
            for (Integer b2:buycarId){
                if (b.getBuycartId()==b2){
                    price=price+b.getProductPrice();
                }
            }
        }
        return price;
    }

    @Override
    public void delatebuycar(Buycart buycart) {
        buycartMapper.deleteByPrimaryKey(buycart.getBuycartId());
    }

    @Override
    public List<BuycarDto> findByuser(int pageNum, int pageSize, int userloginId) {
        PageHelper.startPage(pageNum, pageSize);
        List<BuycarDto> buycarDtos=new ArrayList<>();
        UserLogin userLogin=userLoginMapper.selectByPrimaryKey(userloginId);
        List<Buycart> buycarts=buycartMapper.selectByUserId(userloginId);
        System.out.println(buycarts);
        if (buycarts==null){
            return null;
        }
        else {
            for (Buycart b:buycarts){
                Product product=productMapper.selectByPrimaryKey(b.getProductId());
                System.out.println(product);
                ClassTable classTable=classTableService.selectbyid(product.getProductClassId());
                System.out.println(classTable);
                List<ProductTpicInfo> productTpicInfo=picInfoService.selectbypId(product.getId());
                BuycarDto buycarDto=new BuycarDto();
                buycarDto.setAddTime(new Date());
                buycarDto.setProductNum(b.getProductNum());
                buycarDto.setBrand(classTable.getProductBrand());
                buycarDto.setProductPrice(product.getProductPrice());
                buycarDto.setUrl(productTpicInfo.get(0).getPicUrl());
                buycarDto.setBranddesc(product.getProductDesc());
                buycarDto.setPlace(classTable.getProductPlace());
                buycarDto.setId(b.getBuycartId());
                buycarDtos.add(buycarDto);
            }
            return buycarDtos;
        }

    }

    @Override
    public Buycart findbuid(int buycarid) {
        return buycartMapper.selectByPrimaryKey(buycarid);
    }

}
